home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 889 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.3 KB

  1. From: JdeBP@jba.co.uk (Jonathan de Boyne Pollard)
  2. Message-ID: <4jdr8b$83h@silver.jba.co.uk>
  3. X-Original-Date: 28 Mar 1996 10:56:11 -0000
  4. Path: in2.uu.net!bounce-back
  5. Date: 28 Mar 96 12:54:17 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Quick questions
  9. Organization: JBA Software Products, Studley, England.
  10. References: <4j5b26$1e7a@mule1.mindspring.com> <4j7a64$l9i@engnews1.Eng.Sun.COM> <4jajk2$17aq@mule1.mindspring.com>
  11. Reply-To: JdeBP@donor2.demon.co.uk
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMVqMCeEDnX0m9pzZAQFKlAGAosKD5qhY1Tw24wc3J5Y0WwoNzp1ZzNJr
  15.     LYTyKOebEFl+5zy87v/fGbkG23H96DAp
  16.     =WMeZ
  17.  
  18. Andrew Bell (abell@mindspring.com) wrote:
  19. | No, I'd want to go the other way.  In the case of "pure wrapper"
  20. | classes (as I defined in my earlier post), one might want an automatic
  21. | cast from the original to the wrapper.  In my case, I have created a
  22. | class to wrap around an OS-defined struct, to give it access safety
  23. | (the particular class contains a type specifier and a union) and
  24. | smarter access as well as constructors.
  25.  
  26. | I'd like to have a casting operator that would allow me to convert the
  27. | original struct to the pure wrapper automatically.
  28.  
  29. You do realise that you aren't guaranteed the behaviour that you obviously
  30. want in Standard C++, don't you ?
  31.  
  32. A simple version of your scenario, in code, is :
  33.  
  34.     struct SOMETHING {    // Arbitrary O/S API POD-struct
  35.         int i ;
  36.         char buf[9] ;
  37.     } ;
  38.  
  39.     struct Something : public SOMETHING {
  40.         Something() : i(7) { buf[0] = '\000' ; }
  41.     } ;
  42.  
  43. What you want is to be able to substitute `Something' wherever your O/S
  44. API requires a `SOMETHING'.
  45.  
  46. Unfortunately, Standard C++ doesn't guarantee that the address of a base
  47. class subobject is the same as the address of the complete object.  In other
  48. words, casting from `Something *' to `SOMETHING *' might involve an offset
  49. calculation.  This isn't particularly difficult or bothersome when calling
  50. the O/S API, since the compiler will handle passing a `Something *' to an 
  51. O/S API function that expects a `SOMETHING *'.  
  52.  
  53. However, if an O/S API function _returns_ a `SOMETHING *', then assigning
  54. the result into a `Something *' is not only wrong (Standard C++ will
  55. require you to use a cast) but dangerous.  The danger lies in the fact that
  56. the C++ implementation may require extra housekeeping information in a
  57. `Something' (because it is a derived class, because it has function
  58. members, or for some other reason), which simply won't be in the
  59. `SOMETHING' object the address of which was returned to you by the O/S API.
  60.  
  61. The flaw in your original message was assuming that not having virtual
  62. function members is the only requirement for being a POD-struct.  If you
  63. look at [dcl.init.aggregate] and [class] you will find that there is quite
  64. a long list of prohibitions on POD-structs, including not having base
  65. classes.
  66.  
  67. You can tell that I've trod this road as well, can't you ?  (-:
  68.  
  69. | (but you can't always get what you want...)
  70.  
  71. Exactly.
  72. ---
  73. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  74. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  75. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  76. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  77. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  78.